package com.imon.web.codesphere.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.bson.types.ObjectId;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.internal.util.SVNDate;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Transient;

@Entity
public class RevisionInfo {

	@Id
	private ObjectId id;

	private String repositoryUrl;

	private SVNLogEntry logEntry;

	private final String date;

	private final String author;

	private final Collection<RevisionChangeSet> revisionChanges = new ArrayList<RevisionChangeSet>();

	private final String message;

	private final long revision;

	@Transient
	private final Map revisionMap;

	public RevisionInfo() {
		author = null;
		date = null;
		message = null;
		revision = -1;
		revisionMap = Collections.EMPTY_MAP;
	}

	public RevisionInfo(final String repositoryRoot, final SVNLogEntry aLogEntry) {
		repositoryUrl = repositoryRoot;
		date = SVNDate.formatHumanDate(aLogEntry.getDate(), null);
		author = aLogEntry.getAuthor();
		message = aLogEntry.getMessage();
		revision = aLogEntry.getRevision();
		revisionMap = aLogEntry.getRevisionProperties().asMap();
		populateChangeSet((aLogEntry.getChangedPaths() == null) ? Collections.EMPTY_LIST : aLogEntry.getChangedPaths()
				.values());
	}

	public void populateChangeSet(final Collection<SVNLogEntryPath> logEntries) {
		for (final SVNLogEntryPath logEntryPath : logEntries) {
			final RevisionChangeSet revisionChangeSet = new RevisionChangeSet();
			revisionChangeSet.setChangeType(logEntryPath.getType());
			revisionChangeSet.setPath(logEntryPath.getPath());
			revisionChangeSet.setCopyPath(logEntryPath.getCopyPath());
			revisionChangeSet.setCopyRevision(logEntryPath.getCopyRevision());
			revisionChangeSet.setFileKind(logEntryPath.getKind().toString());
			revisionChanges.add(revisionChangeSet);
		}
	}

	public String getDate() {
		return date;
	}

	public String getAuthor() {
		return author;
	}

	public String getMessage() {
		return message;
	}

	public long getRevision() {
		return revision;
	}

	public SVNLogEntry getLogEntry() {
		return logEntry;
	}

	public void setLogEntry(final SVNLogEntry aLogEntry) {
		this.logEntry = aLogEntry;
	}

	public void setRepositoryUrl(final String aRepositoryUrl) {
		this.repositoryUrl = aRepositoryUrl;
	}

	public String getRepositoryUrl() {
		return repositoryUrl;
	}

	public Collection<RevisionChangeSet> getRevisionChanges() {
		return revisionChanges;
	}

	@Override
	public String toString() {
		return "RevisionInfo [repositoryUrl=" + repositoryUrl + ", logEntry=" + logEntry + ", date=" + date
				+ ", author=" + author + ", revisionChanges=" + revisionChanges.size() + ", message=" + message
				+ ", revision=" + revision + ", revisionMap=" + revisionMap.size() + "]";
	}
}
